package com.laoxu.student.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoxu.student.business.entity.Department;
import com.laoxu.student.business.entity.Department;
import com.laoxu.student.business.service.DepartmentService;
import com.laoxu.student.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  院系控制器
 * </p>
 *
 * @author Laoxu
 * @since 2020-04-04
 */
@Api(description="院系管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("新增或修改院系")
    @PostMapping("save")
    public Result save(@ApiParam(name="department",value = "院系对象",required = true)
                       @RequestBody Department department){
        departmentService.saveOrUpdate(department);
        return Result.ok().data("departmentId",department.getId());
    }

    @ApiOperation("根据ID查询院系")
    @GetMapping("{id}")
    public Result get(@ApiParam(name="id",value = "院系ID",required = true)
                      @PathVariable Long id){
        Department department = departmentService.getById(id);
        return Result.ok().data("item", department);
    }

    @ApiOperation("删除多个院系")
    @PostMapping("removes")
    public Result removes(@ApiParam(name="ids",value = "院系ID数组",required = true)
                          @RequestBody List<Long> ids){
        departmentService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation(value = "院系分页列表")
    @GetMapping("{page}/{limit}")
    public Result pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<Department> pageParam = new Page<>(page, limit);

        departmentService.pageQuery(pageParam);
        List<Department> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  Result.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "院系列表")
    @GetMapping("list")
    public Result list(){
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        List<Department> records = departmentService.list(queryWrapper);

        return  Result.ok().data("items", records);
    }
}

